package ruanjianceshi3;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.zone.ZoneRules;

public class HoldingTimeByZoned {
    private ZonedDateTime startingTime;
    private ZonedDateTime endingTime;

    public HoldingTimeByZoned(ZonedDateTime startingTime, ZonedDateTime endingTime) {
        this.startingTime = startingTime;
        this.endingTime = endingTime;
    }

    public long getHoldingTime() {
        long minute = 0;
        long between;

        ZoneId zoneId = startingTime.getZone();
        ZoneRules zoneRules = zoneId.getRules();

        Boolean isDstOfStart = zoneRules.isDaylightSavings(startingTime.toInstant());
        Boolean isDstOfEnd = zoneRules.isDaylightSavings(endingTime.toInstant());

        between = endingTime.toEpochSecond() - startingTime.toEpochSecond();

        if (between < 0) {
            minute = between / 60;
        } else {
            minute = (between + 59) / 60;
        }
        String cal1 = String.valueOf(startingTime.getMonth());
        String cal2 = String.valueOf(startingTime.getDayOfWeek());
        String cal3 = String.valueOf(endingTime.getMonth());
        String cal4 = String.valueOf(endingTime.getDayOfWeek());

        if(cal1 == "OCTOBER" && startingTime.getDayOfMonth()>= 25 && cal2 == "SUNDAY"){
            //开始时间在十月最后一个周日
            if(startingTime.getHour() == 2 && endingTime.getHour() == 2 && endingTime.getHour()<3 && endingTime.getMinute()<=startingTime.getMinute()) {
                //因为转换而出现通话时间为负的情况
                minute+=60;
            }
            else if(startingTime.getHour()<2 && endingTime.getHour()>=2) {
                //开始时间是两点前，结束时间是两点后（经历第二次转换 由于不知道怎么输出两种可能性的minute故只取了两次经历2-3点时间段的情况
                minute+=60;
            }
            else {
            }
        }
        else {
            if (cal3 == "OCTOBER" && endingTime.getDayOfMonth() >= 25 && cal4 == "SUNDAY") {
                if (endingTime.getHour() >= 2)
                    minute += 60;
            } else {
                if ((cal1 == "MARCH" && startingTime.getDayOfMonth() >= 25 && cal2 == "SUNDAY")) {
                    //开始时间在三月最后一个周日
                    if (startingTime.getHour() < 2 && endingTime.getHour() >= 3)
                        //开始时间在两点前，结束时间在三点后（经过了第一次转换
                        minute -= 60;
                } else {//开始时间不在三月最后一个周日
                    if ((cal3 == "MARCH" && endingTime.getDayOfMonth() >= 25 && cal4 == "SUNDAY")) {
                        //但是结束时间在（从前一天打到这一天
                        if (endingTime.getHour() >= 3)
                            minute -= 60;
                    }
                }
            }
        }

        return minute;
    }
    public static String Money(long minute) {
        double money = 0;
        if(minute>1800 || minute <=0){
            return "通话时间输入错误，请重新输入。";
        }else{
            if(minute<=20)
                money = minute*0.05;
            else
                money = 1+(minute-20)*0.1;
        }
        return  String.valueOf(money);
    }
}
